BETA
Overview
The BETA function computes statistical properties of the beta distribution, a continuous probability distribution defined on the interval [0, 1]. The beta distribution is fundamental in Bayesian statistics, order statistics, and modeling random variables that represent proportions, percentages, or probabilities.
This implementation wraps scipy.stats.beta from the SciPy library, providing access to multiple statistical methods including PDF, CDF, inverse CDF (quantiles), survival function, and descriptive statistics such as mean, median, variance, and standard deviation.
The probability density function (PDF) for the beta distribution is defined as:
f(x; a, b) = \frac{\Gamma(a + b)}{\Gamma(a)\Gamma(b)} x^{a-1}(1-x)^{b-1}
where 0 \le x \le 1, a > 0 and b > 0 are the shape parameters, and \Gamma denotes the gamma function. The shape parameters control the distribution’s form: when a = b = 1, the beta distribution reduces to a uniform distribution; when a, b > 1, the distribution is unimodal; and when a, b < 1, it is U-shaped.
The mean and variance of the standard beta distribution are:
\mu = \frac{a}{a + b}, \quad \sigma^2 = \frac{ab}{(a + b)^2(a + b + 1)}
The function supports location (loc) and scale (scale) parameters to shift and stretch the distribution beyond the standard [0, 1] interval. The transformed distribution has support [loc, loc + scale]. For more details on the beta distribution, see the Wikipedia article on beta distribution.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=BETA(value, a, b, loc, scale, beta_method)
value(float, optional, default: null): Input value for PDF/CDF/SF, or probability for ICDF/ISF methodsa(float, optional, default: 1): First shape parameter (must be > 0)b(float, optional, default: 1): Second shape parameter (must be > 0)loc(float, optional, default: 0): Location parameter for shifting the distributionscale(float, optional, default: 1): Scale parameter for stretching the distribution (must be > 0)beta_method(str, optional, default: “pdf”): Statistical method to compute
Returns (float): Distribution result (float), or error message string.
Examples
Example 1: PDF at x=0.5 with shape params a=2, b=3
Inputs:
| value | a | b | loc | scale | beta_method |
|---|---|---|---|---|---|
| 0.5 | 2 | 3 | 0 | 1 |
Excel formula:
=BETA(0.5, 2, 3, 0, 1, "pdf")
Expected output:
1.5
Example 2: CDF at x=0.5 with shape params a=2, b=3
Inputs:
| value | a | b | loc | scale | beta_method |
|---|---|---|---|---|---|
| 0.5 | 2 | 3 | 0 | 1 | cdf |
Excel formula:
=BETA(0.5, 2, 3, 0, 1, "cdf")
Expected output:
0.6875
Example 3: Inverse CDF (quantile) at probability 0.6875
Inputs:
| value | a | b | loc | scale | beta_method |
|---|---|---|---|---|---|
| 0.6875 | 2 | 3 | 0 | 1 | icdf |
Excel formula:
=BETA(0.6875, 2, 3, 0, 1, "icdf")
Expected output:
0.5
Example 4: Mean of Beta(2, 3) distribution
Inputs:
| a | b | loc | scale | beta_method |
|---|---|---|---|---|
| 2 | 3 | 0 | 1 | mean |
Excel formula:
=BETA(2, 3, 0, 1, "mean")
Expected output:
0.4
Python Code
from scipy.stats import beta as scipy_beta
import math
def beta(value=None, a=1, b=1, loc=0, scale=1, beta_method='pdf'):
"""
Wrapper for scipy.stats.beta distribution providing multiple statistical methods.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html
This example function is provided as-is without any representation of accuracy.
Args:
value (float, optional): Input value for PDF/CDF/SF, or probability for ICDF/ISF methods Default is None.
a (float, optional): First shape parameter (must be > 0) Default is 1.
b (float, optional): Second shape parameter (must be > 0) Default is 1.
loc (float, optional): Location parameter for shifting the distribution Default is 0.
scale (float, optional): Scale parameter for stretching the distribution (must be > 0) Default is 1.
beta_method (str, optional): Statistical method to compute Valid options: PDF, CDF, ICDF, SF, ISF, Mean, Median, Variance, Std Dev. Default is 'pdf'.
Returns:
float: Distribution result (float), or error message string.
"""
valid_methods = {'pdf', 'cdf', 'icdf', 'sf', 'isf', 'mean', 'median', 'var', 'std'}
if not isinstance(beta_method, str):
return "Invalid input: beta_method must be a string."
method = beta_method.lower()
if method not in valid_methods:
return f"Invalid method: {beta_method}. Must be one of {', '.join(sorted(valid_methods))}."
try:
a = float(a)
b = float(b)
loc = float(loc)
scale = float(scale)
except (ValueError, TypeError):
return "Invalid input: a, b, loc, and scale must be numbers."
if a <= 0:
return "Invalid input: a must be > 0."
if b <= 0:
return "Invalid input: b must be > 0."
if scale <= 0:
return "Invalid input: scale must be > 0."
try:
dist = scipy_beta(a, b, loc=loc, scale=scale)
# Methods that require value
if method in ['pdf', 'cdf', 'icdf', 'sf', 'isf']:
if value is None:
return f"Invalid input: missing required argument 'value' for method '{method}'."
try:
value = float(value)
except (ValueError, TypeError):
return "Invalid input: value must be a number."
if method == 'pdf':
result = dist.pdf(value)
elif method == 'cdf':
result = dist.cdf(value)
elif method == 'sf':
result = dist.sf(value)
elif method == 'icdf':
if not (0 <= value <= 1):
return "Invalid input: probability must be between 0 and 1 for icdf."
result = dist.ppf(value)
elif method == 'isf':
if not (0 <= value <= 1):
return "Invalid input: probability must be between 0 and 1 for isf."
result = dist.isf(value)
# Methods that do not require value
elif method == 'mean':
result = dist.mean()
elif method == 'median':
result = dist.median()
elif method == 'var':
result = dist.var()
elif method == 'std':
result = dist.std()
except Exception as e:
return f"scipy.stats.beta error: {str(e)}"
if isinstance(result, float):
if math.isnan(result):
return "Result is NaN"
if math.isinf(result):
return "inf" if result > 0 else "-inf"
return float(result)